<?php

namespace app\admin\controller;
use think\Db;
use app\common\service\BalanceService;
class Out extends Common{
	/*
	  提现记录
	 */

	public function index() {
		return $this->fetch();
	}
	
	public function ewm() {
		$address = request()->param('address');
		$this->assign('address',$address);
		return $this->fetch();
	}

	public function dataList() {
		$limit = request()->param('limit') ?: 20;
		$username = request()->param('username');
		$uid = request()->param('uid');
		$status = request()->param('status');
		$coin_name   = request()->param('coin_name');
		$wallet_name   = request()->param('wallet_name');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if($status != '') {
            $where .= ' and t.status='.$status;
        }
        if($uid != '') {
			if (!in_array($uid, $this->data_uids_arr)) {
				return $this->tableData([]);
			}
			$where .= ' and t.uid='.$uid;
		} else {
			if ($this->data_uids_string) {
				$where .= ' and t.uid in ('.$this->data_uids_string.')';
			}
		}
        if ($coin_name != '') {
            $where .= ' and c.name like \'%' . $coin_name . '%\'';
        }
        if ($wallet_name != '') {
            $where .= ' and w.name like \'%' . $wallet_name . '%\'';
        }
        if($start_time != '') {
            $where .= ' and t.date>="'.$start_time.'"';
        }
		if($end_time != '') {
            $where .= ' and t.date<="'.$end_time.'"';
        }
		$data = Db::table('tp_out_record')
				->alias('t')
				->join('tp_user u', 'u.id = t.uid', 'LEFT')
				->join('tp_coin_wallet w', 'w.id = t.wid', 'LEFT')
				->join('tp_coin c', 'c.id = t.cid', 'LEFT')
				->whereTime('t.date','>=','2021-05-16 00:00:00')
				->where($where)
				->order('t.id desc,t.status asc,t.fee desc')
				->field('t.*,u.username,u.phone,c.name coin_name,w.name wallet_name')
				->paginate($limit);
		$data = $data->toArray();
		if(empty($data['data'])){
			$data['data']  = Db::table('tp_out_record')
				->alias('t')
				->join('tp_user u', 'u.id = t.uid', 'LEFT')
				->join('tp_coin_wallet w', 'w.id = t.wid', 'LEFT')
				->join('tp_coin c', 'c.id = t.cid', 'LEFT')
				->whereTime('t.date','>=','2021-05-16 00:00:00')
				->where($where)
				->order('t.id desc,t.status asc')
				->field('t.*,u.username,u.phone,c.name coin_name,w.name wallet_name')
				->page(1)
				->limit($limit)
				->select();		
		}
		return $this->tableData($data);
	}

	
	public function excel(){
		$limit = request()->param('limit') ?: 20;
		$username = request()->param('username');
		$uid = request()->param('uid');
		$status = request()->param('status');
		$coin_name   = request()->param('coin_name');
		$wallet_name   = request()->param('wallet_name');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if($status != '') {
            $where .= ' and t.status='.$status;
        }
        if($uid != '') {
            $where .= ' and t.uid='.$uid;
        }
        if ($coin_name != '') {
            $where .= ' and c.name like \'%' . $coin_name . '%\'';
        }
        if ($wallet_name != '') {
            $where .= ' and w.name like \'%' . $wallet_name . '%\'';
        }
        if($start_time != '') {
            $where .= ' and t.date>="'.$start_time.'"';
        }
		if($end_time != '') {
            $where .= ' and t.date<="'.$end_time.'"';
        }
		$data  = Db::table('tp_out_record')
				->alias('t')
				->join('tp_user u', 'u.id = t.uid', 'LEFT')
				->join('tp_coin_wallet w', 'w.id = t.wid', 'LEFT')
				->join('tp_coin c', 'c.id = t.cid', 'LEFT')
				->whereTime('t.date','>=','2021-05-16 00:00:00')
				->where($where)
				->order('t.id desc')
				->field('t.uid,u.username,w.name wallet_name,c.name coin_name,t.amount,t.fee,t.left_amount,t.address,t.tag,t.status,t.tag,t.date')
				->select();
		return exportExcel(['用户编号','用户名','钱包名称','提币币种','总扣除数量','扣除手续费','提币数量','提币地址','标签','状态(1=通过，0=待审核，-1=已拒绝)','提币时间'], $data);
	}
	/*
	  提币审核
	 */
	public function pass() {
		if (request()->method() == 'POST') {
			if (!in_array($this->admin_info['level'], [1])) {
				return ['code' => 1, 'msg' => '权限不足'];
			}
			$data = [];
			$id = intval(request()->param('id'));
			$remark = request()->param('value');
			
			Db::startTrans();
			$info = Db::table('tp_out_record')->where('id', $id)->lock(true)->find();
			if ($info['status'] != 0) {
				Db::rollback();
				return ['code' => 1, 'msg' => '订单状态错误'];
			}
			$res = Db::table('tp_out_record')->where('id', $id)->update([
				'status'=>1,
				'remark' => '已通过',
				'check_remark'=>$remark,
				'change_date'=>date('Y-m-d H:i:s')
			]);
			if (!$res) {
				Db::rollback();
				return ['code' => 1, 'msg' => '操作失败'];
			}
			Db::commit();
			return ['code' => 0, 'msg' => '操作成功'];
		}
	}
	// public function bf() {
	// 	if (request()->method() == 'POST') {
	// 		$data = [];
	// 		$id = intval(request()->param('id'));
	// 		Db::startTrans();
	// 		$info = Db::table('tp_out_record')->where('id', $id)->lock(true)->find();
	// 		if ($info['status'] != 1) {
	// 			Db::rollback();
	// 			return ['code' => 1, 'msg' => '订单状态错误'];
	// 		}
	// 		if ($info['tag'] != 'ERC20') {
	// 			Db::rollback();
	// 			return ['code' => 1, 'msg' => '币种错误'];
	// 		}

	// 		$post_data = [
	// 				'pass'=>'smdhelidie145dd',
	// 				'to'=>$info['address'],
	// 				'amount'=>$info['left_amount'],
	// 				'sign'=>md5('smdhelidie145dd'.$info['address'].'doIeiEJazaG78EDazaEs')
	// 			];
	// 		$res = curl('http://47.241.104.184/v1/api/erc20_trade?ssr=1',$post_data,'post');

	// 		$res = json_decode($res,1);
	// 		if ($res['code'] == 400) {
	// 			Db::table('tp_out_record')->where('id', $id)->update([
	// 				'status'=>0,
	// 				'remark' => '待审核',
	// 				'check_remark'=>'',
	// 				// 'hash'=>'',
	// 				'change_date'=>date('Y-m-d H:i:s')
	// 			]);
	// 			Db::rollback();
	// 			return ['code' => 1, 'msg' => '操作失败'];
	// 		} else {
	// 			Db::table('tp_out_record')->where('id', $id)->update([
	// 					'hash'=>$res['msg']
	// 			]);
	// 		}
	// 		Db::commit();
	// 		return ['code' => 0, 'msg' => '操作成功'];
	// 	}
	// }

	public function pass2() {
		if (request()->method() == 'POST') {
			if (!in_array($this->admin_info['level'], [1])) {
				return ['code' => 1, 'msg' => '权限不足'];
			}
			$data = request()->param('data');
			$remark = request()->param('value');		
			$data = json_decode($data,1);
			if ($data) {
				$ids = array_column($data, 'id');
				$has = Db::table('tp_out_record')->where('id','in',$ids)->vhere('status','<>',0)->find();
				if ($has) {
					return ['code' => 1, 'msg' => '存在已审核过的订单'];
				}
				$res = Db::table('tp_out_record')->where('id','in',$ids)->update([
					'status'=>1,
					'remark' => '已通过',
					'check_remark'=>$remark,
					'change_date'=>date('Y-m-d H:i:s')
				]);
				if (!$res) {
				// Db::rollback();
				return ['code' => 1, 'msg' => '操作失败'];
			}
			// Db::commit();
			return ['code' => 0, 'msg' => '操作成功'];
			}
		}
	}

	/*
	  提币拒绝
	 */
	public function refuse() {
		if (request()->method() == 'POST') {
			if (!in_array($this->admin_info['level'], [1])) {
				return ['code' => 1, 'msg' => '权限不足'];
			}
			$data = [];
			$id = intval(request()->param('id'));
			$remark = request()->param('value');
			$info = Db::table('tp_out_record')->where('id', $id)->find();
			if ($info['status'] != 0) {
				return ['code' => 1, 'msg' => '订单状态错误'];
			}
			Db::startTrans();
			$res1 = Db::table('tp_out_record')->where('id', $id)->update([
				'status'=>-1,
				'remark' =>'已拒绝',
				'check_remark'=>$remark,
				'change_date'=>date('Y-m-d H:i:s')
			]);
			$res2 = BalanceService::updateBalance($info['uid'], $info['wid'], $info['amount'], '提币驳回', 23,$info['id']);
			if ($res1 && $res2) {
				Db::commit();
				return ['code' => 0, 'msg' => '操作成功'];
			} else {
				Db::rollback();
				return ['code' => 1, 'msg' => '操作失败'];
			}
		}
	}

	public function refuse2() {
		if (request()->method() == 'POST') {
			if (!in_array($this->admin_info['level'], [1])) {
				return ['code' => 1, 'msg' => '权限不足'];
			}
			$data = request()->param('data');
			$remark = request()->param('value');		
			$data = json_decode($data,1);
			if ($data) {
				Db::startTrans();
				$ids = array_column($data, 'id');
				$has = Db::table('tp_out_record')->where('id','in',$ids)->vhere('status','<>',0)->find();
				if ($has) {
					return ['code' => 1, 'msg' => '存在已审核过的订单'];
				}
				$date = date('Y-m-d H:i:s');
				foreach ($data as $key => $val) {
					$info = $val;
					$res = BalanceService::updateBalance($info['uid'], $info['wid'], $info['amount'], '提币驳回', 23,$info['id']);
					if (!$res) {
						Db::rollback();
						return ['code' => 1, 'msg' => '操作失败'];
					}
				}
				$res = Db::table('tp_out_record')->where('id','in',$ids)->update([
					'status'=>-1,
					'remark' =>'已拒绝',
					'check_remark'=>$remark,
					'change_date'=>date('Y-m-d H:i:s')
				]);
					
				if ($res) {
					Db::commit();
					return ['code' => 0, 'msg' => '操作成功'];
				} else {
					Db::rollback();
					return ['code' => 1, 'msg' => '操作失败'];
				}
			}
		}
	}

}